#include <stdio.h>
#include <string.h>

#include "..//include//createvfs.h"
#include "..//include//n_ary.h"
#include "..//include//hash.h"
#include "..//include//fileOperations.h"

struct Block b;
struct header headervar;
struct FD file_desc;	

//FILE *fp2_src; 
//FILE *fp2_dest;


int movefileop(char source_file_path[], char dest_file_path[], char filename_g[]){

	//int blkno;	
	char completePath[100]="";
	char *fname;
	char *fpath;
	
	int j;
	int flag_src;
	int flag_dest;
	
	 
	FILE *fp2_dest;
	
	flag_src=1;
	flag_dest=1;
	
	fname = get_fname(source_file_path);
	fpath = get_abspath(dest_file_path);
	
	//printf("%s\n",fpath);
	
	//printf("%s, %s\n",fname,fpath);
	//fname = "";
	//fpath = "";
	
	if ((strcmp(source_file_path,"") == 0) || (strcmp(dest_file_path,"") == 0)){
		printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_00);
		return 0;
	}
	
	strcpy(completePath, "");
	strcat(completePath, dest_file_path); // Includes the file name
	//strcat(completePath, "/");
	//strcat(completePath, fname);
	strcat(completePath, "\0");
	
	//printf("Complete path: %s\n",completePath);

	fp2_dest = fopen(filename_g, "rb+");
	
	if (fp2_dest == NULL){ 
		//printf("movefile_FAILURE ");
		return 0;
	}
		
	else{
	
		rewind (fp2_dest);
		fseek (fp2_dest, (LABEL_SIZE*sizeof(char) + 2*(sizeof(int)) + sizeof(struct FD)), SEEK_SET);
		
				for(j=2; j<=TOTAL_DESC; j++){
			 		fread (&file_desc, sizeof(struct FD), 1, fp2_dest);
			 		
			 		if(strcmp(file_desc.path,fpath)==0){			 			
			 			flag_dest=0;			 			
			 		} 	
			 		
			 		if(strcmp(file_desc.path,source_file_path)==0){			 			
			 			flag_src=0;
			 		} 		 		
			 				 		
			 	}
			 	
		if(flag_src!=0){
			printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_01);
			return 0;		
		}
		
		else if(flag_dest!=0){
			printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_02);
			return 0;		
		}
		
		else if(strcmp(source_file_path,"/") == 0){
			printf("movefile_FAILURE %s\n",ERR_VFS_MOVEFILE_01);
			return 0;		
		}
			 	
		else{	 	
			rewind (fp2_dest);
			fseek (fp2_dest, (LABEL_SIZE*sizeof(char) + 2*(sizeof(int)) + sizeof(struct FD)), SEEK_SET);
		
			 	for(j=2; j<=TOTAL_DESC; j++){
			 		fread (&file_desc, sizeof(struct FD), 1, fp2_dest);
			 		
			 		if(strcmp(file_desc.path,source_file_path)==0){
			 			
			 			//blkno = file_desc.blockno;			 			
			 			
  						strcpy(file_desc.path,completePath);
  						
  				
  					fseek (fp2_dest, (LABEL_SIZE*sizeof(char) + 2*(sizeof(int))+ sizeof(struct FD)*(j-1)), SEEK_SET); 
		  			fwrite(&file_desc, sizeof(struct FD), 1, fp2_dest);
  					rewind (fp2_dest);  						
  							
			 		break;
			 		} 		
			 		
			 				 		
			 	}
			 	
			// Update in the nary tree	 	
			 	
			root = del_nary(root,source_file_path);
			//printf("\nDeleted?????: %d",searchTree(root, source_file_path));
			
			if(strcmp(completePath,"/")!=0)
				root = insert(root,completePath);
			
			deletefile(source_file_path);
			hashfile(fname,fpath);
			
		}
		
	}
	
	/*// Debug
	
	rewind (fp2_dest);
	fseek (fp2_dest, (LABEL_SIZE*sizeof(char) + 2*(sizeof(int))), SEEK_SET);
		
	for(j=1; j<=TOTAL_DESC; j++){
		fread (&file_desc, sizeof(struct FD), 1, fp2_dest);
		printf("Inside movefile: %s, %s\n",file_desc.path, dest_file_path);
		if(strcmp(file_desc.path,dest_file_path)==0) break;
	}*/
	
	fclose(fp2_dest);
	
	return 1;
	
}


char *get_fname(char path[]){

	int len,j,i,k;
	char l;	
	static char new_str[20];
	k=0;
	
	
	len=strlen(path);
	j=len-1;
	while(path[j]!='/')
	{
		l=path[j];		
		j--;
	}
	
	for(i=j+1; i<len;i++){
		new_str[k]=path[i];
		k++;
	}
		
	new_str[k] = '\0';
	
	return new_str;
	
}


char *get_abspath(char path[]){

	int len,j,i,k;
	char l;	
	static char new_str[20];
	k=0;
	
	
	len=strlen(path);
	j=len-1;
	while(path[j]!='/')
	{
		l=path[j];		
		j--;
	}
	
	for(i=0; i<j+1;i++){
		new_str[k]=path[i];
		k++;
	}
		
	new_str[k] = '\0';
	
	return new_str;
	
}

